﻿using OpenTK.Graphics;
using OpenTK.Math;
using System.Drawing;
using Rekx.Graphics;

namespace Rekx.Pong
{
    class Ball
    {
        
        private OpenTK.Math.Vector3 position;
        private float width = 0.5f;
        public Vector3 velocity;
        public int speed = 13;

        DrawableComponent drawComponent = null;

        public Vector3 Position
        {
            get
            {
                return position;
            }
        }

        public DrawableComponent DrawComponent
        {
            get
            {
                return drawComponent;
            }
        }


        public Ball()
        {
            Restart();

            // setup vertex and index arrays
            Vector3[] vertices = new Vector3[]
            {
                new Vector3( - width / 2, 0.1f,  + width/2),
                new Vector3( + width / 2, 0.1f,  + width/2),
                new Vector3( - width / 2, 0.1f,  - width/2),
                new Vector3( + width / 2, 0.1f,  - width/2)
            };

            int[] indices = new int[]
            {
                0,
                1,
                2,
                3
            };

            drawComponent = new DrawableComponent(vertices, indices);
        }

       
        public void Update(float ms)
        {
            // make sure that it is dependent on time
            position += (velocity*ms);
        }

        public void Restart()
        {
            position = new Vector3();


            // randomly generate the direction of the vector
            System.Random rand = new System.Random();

            Vector3 direction = new Vector3(rand.Next(1, 100) / 100.0f, 0.0f, rand.Next(30, 100) / 100.0f);

            direction.Normalize(); // normalize so that we can make sure the speed is consitent.
            velocity = direction * speed;
        }
    }
}
